package DBIX::DBClass;

use strict;
use warnings;
use DBI;


sub new
{
    my $class = shift;
    my $parm = shift;
    my $this = {};
    $this->{'dbm'} = $parm->{'dbm'};
    $this->{'database'} =  $parm->{'database'};
    $this->{'host'} = $parm->{'host'};
    $this->{'port'} = $parm->{'port'};
    $this->{'user'} = $parm->{'user'};
    $this->{'passwd'} = $parm->{'passwd'};
    bless $this, $class;
    return $this;
}

sub _set_database
{
	my $self = shift;
	$self->{'database'} = shift;
}

sub _set_dbhost
{
	my $self = shift;
	$self->{'host'} = shift;
}

sub _set_dbport
{
	my $self = shift;
	$self->{'port'} = shift;
}

sub _set_dbuser
{
	my $self = shift;
	$self->{'user'} = shift;
}

sub _set_passwd
{
	my $self = shift;
	$self->{'passwd'} = shift;
}

sub _connect
{
    my $self = shift;
    my $dsn = qq|DBI:$self->{'dbm'}:database=$self->{'database'};
            host=$self->{'host'};
            port=$self->{'port'};
    |;

    my $dbh = DBI->connect(
        $dsn,
        $self->{'user'},
        $self->{'passwd'},
        {
            AutoCommit => 1,
            RaiseError => 1,
            PrintError => 0,
        }
    ) or die DBI->errstr();
    $self->{'dbh'} = $dbh;
    return $self->{'dbh'};
}

sub _getconnect
{
    my $self = shift;
    return $self->{'dbh'};
}

sub _disconnect
{
    my $self = shift;
    $self->{'dbh'}->disconnect();
}

sub _checktable
{
    my $self = shift;
    my $tablename = shift;
    my $regex     = qr/$tablename/;
    my $sth       = $self->{'dbh'}->table_info( '', 'public', undef, 'TABLE');
    for my $rel ( @{ $sth->fetchall_arrayref( {} ) } ) {
        return $rel->{TABLE_NAME} if $rel->{TABLE_NAME} =~ m/$regex/;
    }
    return undef;
}

sub _createindex {
    my $self = shift;
    my ( $tablename, $index ) = @_;
    my $indexname = $tablename . '_' . $index;
    my $SQL       = qq{
        create index  $indexname
        on $tablename($index)
    };
    my $sth = $self->{'dbh'}->prepare($SQL);
    $sth->execute() or die $self->{'dbh'}->errstr;
    return undef;
}



1;
